﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure.Data;
using Keepo.Helpers;
using AlbumDesigner.Helpers;

namespace Keepo.Models.Services
{
    public class DesignResourcesLogic
    {

        static public bool IsConnectedToFacebook()
        {
            return FacebookHelper.isConnected();
        }

        static public DesignImageExport ImportImageFromFacebook(string facebookImageIdStr)
        {
            long facebookImageId = Int64.Parse(facebookImageIdStr);

            using (GenericRepository repository = new GenericRepository())
            {
                DesignImage designImage;

                designImage = repository.First<DesignImage>(m => m.ExternalId == facebookImageId);

                if (designImage == null)
                {
                    designImage = FacebookHelper.ImportImageFromFacebook(facebookImageId);
                }

                return (designImage == null) ? null : new DesignImageExport(designImage);
            }
        }

        static public DesignVectorExport[] getAllVectors()
        {
            using (GenericRepository repository = new GenericRepository())
            {
                var vectors = repository.GetAll<DesignVector>();
                var array = vectors.Select(m => new DesignVectorExport(m)).ToArray();
                return array;
            }
        }

        static public DesignImageExport[] getAllImages()
        {
            using (GenericRepository repository = new GenericRepository())
            {
                var images = repository.GetAll<DesignImage>();
                var array = images.Select(m => new DesignImageExport(m)).ToArray();
                return array;
            }
        }

        static public Dictionary<string, object>[] getVectorCategories()
        {
            using (GenericRepository repository = new GenericRepository())
            {
                Dictionary<string, object>[] array;
                int idx = 0;

                var categories = repository.GetAll<VectorCategory>();

                array = new Dictionary<string, object>[categories.Count()];

                foreach (var cat in categories)
                {
                    Dictionary<string, object> dic = new Dictionary<string, object>();

                    dic["key"] = cat.VectorCategoryId;
                    dic["english"] = cat.Title;
                    dic["hebrew"] = cat.TitleHebrew;

                    array[idx] = dic;

                    idx++;
                }

                return array;
            }
        }

        public const int UserSourceId = 1;
        public const int KeepoSourceId = 2;
        public const int FacebookSourceId = 3;

        static public Dictionary<string, object>[] getImageCategories(int sourceId)
        {
            using (GenericRepository repository = new GenericRepository())
            {
                List<ImageCategory> categories = null;

                switch (sourceId)
                {
                    case UserSourceId:
                        return null;

                    case KeepoSourceId:
                        categories = repository.Find<ImageCategory>(m => m.ImageCategoryId != 1).ToList();
                        break;

                    case FacebookSourceId:
                        return FacebookHelper.GetAlbumsFromFacebook();                        
                }

                if (categories == null)
                {
                    return null;
                }

                List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();

                foreach (var cat in categories)
                {
                    Dictionary<string, object> dic = new Dictionary<string, object>();

                    dic["key"] = cat.ImageCategoryId;
                    dic["english"] = cat.Title;
                    dic["hebrew"] = cat.TitleHebrew;

                    list.Add(dic);
                }

                return list.ToArray();
            }
        }

        static public DesignImageExport[] getUserImages()
        {
            using (GenericRepository repository = new GenericRepository())
            {
                DesignImageExport[] images;

                //int usersCategoryId = 1;

                if (UserHelper.IsLogon())
                {
                    int userId = UserHelper.CurrentUser().UserId;
                    var linqImages = repository.Find<DesignImage>(m => m.ImageCategoryId == 1 && m.UserId == userId && !m.IsRemoved);

                    images = linqImages.ToArray().Select(image => new DesignImageExport(image)).ToArray();
                }
                else
                {
                    string cookieGuid = CookieGuidAttribute.getGuid();
                    var linqImages = repository.Find<DesignImage>(m => m.ImageCategoryId == 1 && m.CookieGuid == cookieGuid);

                    images = linqImages.ToArray().Select(image => new DesignImageExport(image)).ToArray();
                }

                return images;
            }

        }

        static DesignImageExport[] getImagesByCategoryId(long categoryId)
        {
            using (GenericRepository repository = new GenericRepository())
            {
                var designImages = repository.Find<DesignImage>(m => m.ImageCategoryId == categoryId);
                return designImages.ToArray().Select(image => new DesignImageExport(image)).ToArray();
            }
        }

        static public DesignImageExport[] getImagesBySourceAndCategory(int sourceId, long categoryId, string categoryStr)
        {
            switch (sourceId)
            {
                case UserSourceId:
                    return getUserImages();

                case KeepoSourceId:
                    return getImagesByCategoryId(categoryId);

                case FacebookSourceId:
                    return FacebookHelper.GetPhotosByAlbumId(categoryStr);

                default:
                    return null;
            }
        }

        static public DesignVectorExport[] getVectorsByCategoryId(int categoryId)
        {
            using (GenericRepository repository = new GenericRepository())
            {
                var vectors = repository.Find<DesignVector>(m => m.VectorCategoryId == categoryId);

                var results = vectors.ToArray().Select(vector => new DesignVectorExport(vector));

                return results.ToArray();
            }
        }

        static public DesignVectorExport getDesignVectorById(int vectorId)
        {
            using (GenericRepository repository = new GenericRepository())
            {
                DesignVector designVector = repository.GetByKey<DesignVector>(vectorId);

                return new DesignVectorExport(designVector);
            }
        }

        static public DesignImageExport getDesignImageById(long imageId)
        {
            using (GenericRepository repository = new GenericRepository())
            {
                DesignImage image = repository.GetByKey<DesignImage>(imageId);

                return (image == null) ? null : new DesignImageExport(image);
            }
        }

        static public long addDesignImage(DesignImageExport imageExport)
        {
            // Verify that image exists
            if (!FilesHelper.FileExistsOnServer(imageExport.URL))
            {
                return -1;
            }

            try
            {
                var image = imageExport.toEntity();
                ImageHelper.addDesignImage(image);
                return image.DesignImageId;
            }
            catch
            {
                return -1;
            }
        }

        static public DesignFontExport[] getAllFonts()
        {
            using (GenericRepository repository = new GenericRepository())
            {
                var fonts = repository.GetAll<DesignFont>();
                return fonts.Select(m => new DesignFontExport(m)).ToArray();
            }
        }

        static public bool DeleteDesignImage(long imageId)
        {
            using (GenericRepository repository = new GenericRepository())
            {
                var image = repository.GetByKey<DesignImage>(imageId);

                if(image!=null)
                {
                    image.IsRemoved = true;
                    repository.UnitOfWork.SaveChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

    }
}
